package cn.edkuir.order.web;

import cn.edkuir.order.service.OrderService;
import cn.edkuir.order.vo.OrderConfirmVo;
import cn.edkuir.order.vo.OrderSubmitVo;
import cn.edkuir.order.vo.SubmitRespVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.servlet.mvc.support.RedirectAttributes;

import java.util.concurrent.ExecutionException;

@Controller
public class OrderWebController {
    @Autowired
    private OrderService orderService;


    //订单页
    @GetMapping("/toTrade")
    public String toTrade(Model model) throws ExecutionException, InterruptedException {
        OrderConfirmVo orderConfirmVo = orderService.confirmOrder();
        System.out.println(orderConfirmVo);
        model.addAttribute("order", orderConfirmVo);

        return "confirm";
    }


    //下单
    @PostMapping("/submitOrder")
    public String submitOrder(OrderSubmitVo orderSubmitVo, Model model, RedirectAttributes redirectAttributes) {
        System.out.println("订单提交的数据: " + orderSubmitVo);
        SubmitRespVo submitRespVo = orderService.submitOrder(orderSubmitVo);

        if(submitRespVo.getCode() == 0) {
            //下单成功, 去支付选择页
            model.addAttribute("submitResp", submitRespVo);
            return "pay";
        }
//        String msg = "下单失败: ";
//        switch (submitRespVo.getCode()) {
//            case 1: msg += "订单信息过期, 请刷新再次提交";
//            case 2: msg += "订单商品价格发生变化, 请确认后再次提交";
//            case 3: msg += "商品库存不足";
//        }
//        redirectAttributes.addFlashAttribute("msg", msg);
        return "redirect:http://order.mall.com/toTrade";
    }

}
